package cn.maxwell._01_tran;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者对象
        TransactionMQProducer producer = new TransactionMQProducer("tran-producer-group");
        //2.设置namesrv地址
        producer.setNamesrvAddr("localhost:9876");
        //设置该生产者使用的事务监听器
        producer.setTransactionListener(new MyTransactionListener());
        //3.启动Producer实例
        producer.start();
        //4.设置消息
        Message message = new Message("tran", "hello", "817稻米节快乐！！！".getBytes(StandardCharsets.UTF_8));
        //5.发送消息
        SendResult send = producer.sendMessageInTransaction(message,"我是一个参数哦，并且能被事物监听器接收!");
        System.out.println(send);
        //6.关闭
        producer.shutdown();
    }
}
